package com.liuzhu.chaoshikucungunli.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.cardview.widget.CardView;
import com.liuzhu.chaoshikucungunli.R;
import android.animation.ValueAnimator;
import android.view.animation.DecelerateInterpolator;
import androidx.core.content.ContextCompat;
import android.widget.Toast;
import android.os.Handler;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class StatisticsFragment extends Fragment {

    private TextView inboundAmount;
    private TextView outboundAmount;
    private TextView inventoryCount;
    private TextView receivableAmount;
    private TextView payableAmount;
    private SwipeRefreshLayout swipeRefresh;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_statistics, container, false);
        
        initViews(view);
        setupClickListeners(view);
        loadData();
        
        return view;
    }
    
    private void initViews(View view) {
        inboundAmount = view.findViewById(R.id.inbound_amount);
        outboundAmount = view.findViewById(R.id.outbound_amount);
        inventoryCount = view.findViewById(R.id.inventory_count);
        receivableAmount = view.findViewById(R.id.receivable_amount);
        payableAmount = view.findViewById(R.id.payable_amount);
        swipeRefresh = view.findViewById(R.id.swipe_refresh);
        swipeRefresh.setOnRefreshListener(() -> {
            loadData();
            swipeRefresh.setRefreshing(false);
        });
    }
    
    private void setupClickListeners(View view) {
        // 入库管理
        CardView inboundCard = view.findViewById(R.id.card_inbound);
        inboundCard.setClickable(true);
        inboundCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        inboundCard.setOnClickListener(v -> {
            // TODO: 跳转到入库管理界面
            Toast.makeText(requireContext(), "进入入库管理", Toast.LENGTH_SHORT).show();
        });
        
        // 出库管理
        CardView outboundCard = view.findViewById(R.id.card_outbound);
        outboundCard.setClickable(true);
        outboundCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        outboundCard.setOnClickListener(v -> {
            Toast.makeText(requireContext(), "进入出库管理", Toast.LENGTH_SHORT).show();
        });
        
        // 库存查询
        CardView inventoryCard = view.findViewById(R.id.card_inventory);
        inventoryCard.setClickable(true);
        inventoryCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        inventoryCard.setOnClickListener(v -> {
            Toast.makeText(requireContext(), "进入库存查询", Toast.LENGTH_SHORT).show();
        });
        
        // 应收款项
        CardView receivableCard = view.findViewById(R.id.card_receivable);
        receivableCard.setClickable(true);
        receivableCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        receivableCard.setOnClickListener(v -> {
            Toast.makeText(requireContext(), "进入应收款项", Toast.LENGTH_SHORT).show();
        });
        
        // 应付款项
        CardView payableCard = view.findViewById(R.id.card_payable);
        payableCard.setClickable(true);
        payableCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        payableCard.setOnClickListener(v -> {
            Toast.makeText(requireContext(), "进入应付款项", Toast.LENGTH_SHORT).show();
        });
        
        // 客户报价
        CardView customerPriceCard = view.findViewById(R.id.card_customer_price);
        customerPriceCard.setClickable(true);
        customerPriceCard.setForeground(ContextCompat.getDrawable(requireContext(), R.drawable.card_ripple));
        customerPriceCard.setOnClickListener(v -> {
            Toast.makeText(requireContext(), "进入客户报价", Toast.LENGTH_SHORT).show();
        });
    }
    
    private void loadData() {
        try {
            // 显示加载进度
            showLoading(true);
            
            // 模拟网络延迟
            new Handler().postDelayed(() -> {
                // 入库数量动画
                animateValue(0f, 50f, animation -> {
                    float value = (float) animation.getAnimatedValue();
                    inboundAmount.setText(String.format("入库%d件", Math.round(value)));
                });
                
                // 出库数量动画
                animateValue(0f, 30f, animation -> {
                    float value = (float) animation.getAnimatedValue();
                    outboundAmount.setText(String.format("出库%d件", Math.round(value)));
                });
                
                // 库存数量动画
                animateValue(0f, 1000f, animation -> {
                    float value = (float) animation.getAnimatedValue();
                    inventoryCount.setText(String.format("%d件", Math.round(value)));
                });
                
                // 应收款动画
                animateValue(0f, 3456.78f, animation -> {
                    float value = (float) animation.getAnimatedValue();
                    receivableAmount.setText(String.format("¥%.2f", value));
                });
                
                // 应付款动画
                animateValue(0f, 2345.67f, animation -> {
                    float value = (float) animation.getAnimatedValue();
                    payableAmount.setText(String.format("¥%.2f", value));
                });
                    
                // 隐藏加载进度
                showLoading(false);
            }, 1000);
            
        } catch (Exception e) {
            // 处理错误
            showError("加载数据失败：" + e.getMessage());
            showLoading(false);
        }
    }

    private void animateValue(float start, float end, ValueAnimator.AnimatorUpdateListener listener) {
        ValueAnimator animator = ValueAnimator.ofFloat(start, end);
        animator.setDuration(1000);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(listener);
        animator.start();
    }

    private void showLoading(boolean show) {
        if (show) {
            // TODO: 显示加载进度条
        } else {
            // TODO: 隐藏加载进度条
        }
    }

    private void showError(String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
    }
} 